namespace CodeConsult.Nordic.nRF8001.ApplicationControlInterface.Commands
{
    public abstract class AciCommandBase
    {
        protected static EncodeHelper EncodeHelper { get; } = new EncodeHelper();

        protected abstract void EncodeBuffer();

        public bool Encoded { get; protected set; }
        public bool Error { get; protected set; }

        public virtual AciExchangeMessage Encode()
        {
            EncodeBuffer();

            Error = Error || EncodeHelper.Error;
            Encoded = true;

            return Error ? null : new AciExchangeMessage(EncodeHelper.Buffer);
        }

        public static void EncodeBufferWithSimpleCommand(byte opCode)
        {
            EncodeBufferWithCommandHeader(opCode);
        }

        public static void EncodeBufferWithCommandHeader(byte opCode, int length = 0)
        {
            EncodeHelper.Buffer = new byte[length + 2];
            EncodeHelper.SetByte((byte)(length + 1));
            EncodeHelper.SetByte(opCode);
        }

        public override string ToString()
        {
            return GetType().Name;
        }
    }
}
